﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Session = System.Web.SessionState.HttpSessionState;
using System.Web.Configuration;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;

//using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.Reporting.Web
{
    public partial class StaticReportViewer : System.Web.UI.Page
    {
        private string stSessID;
        private string stRptName;
        private string stRptDir;
        private string stServer;
        private string stUser;
        private string stPassword;
        private string stDatabase;
        private ConnectionInfo conn;

        ReportDocument cryRpt = new ReportDocument();

        protected void Page_Init(object sender, EventArgs e)
        {
            buildConnection();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                buildConnection();
                this.parseVars();
                this.configReport();
                this.crViewer.ReportSource = this.cryRpt;
                this.setDBLogonForViewer();
                this.crViewer.DataBind();
            }
        }

        private void parseVars()
        {
            stSessID = this.Request.QueryString["sessID"];
            stRptName = this.Request.QueryString["rptName"];
            if (StringManager.IsEmpty(stRptName))
                stRptName = "TestODBC";
        }

        protected void buildConnection()
        {
            stServer = WebConfigurationManager.AppSettings.Get("DBServer");
            stUser = WebConfigurationManager.AppSettings.Get("User");
            stPassword = WebConfigurationManager.AppSettings.Get("Password");
            stDatabase = WebConfigurationManager.AppSettings.Get("Database");

            conn = new ConnectionInfo();
            conn.DatabaseName = stDatabase;
            conn.ServerName = stServer;
            conn.UserID = stUser;
            conn.Password = stPassword;

        }

        private void configReport()
        {
            stRptDir = WebConfigurationManager.AppSettings.Get("ReportDir");
            string stRptPath = Server.MapPath(stRptDir + "\\" + stRptName + ".rpt");
            if (!FileManager.Exists(stRptPath))
                return;

            cryRpt.Load(stRptPath);
            //SetDBLogonForReport(conn, cryRpt);
        }

        private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
        {
            Tables tables = reportDocument.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
            {
                TableLogOnInfo tableLogonInfo = table.LogOnInfo;
                tableLogonInfo.ConnectionInfo = connectionInfo;
                table.ApplyLogOnInfo(tableLogonInfo);
            }
        }

        private void setDBLogonForViewer()
        {
            for (int i = 0; i < this.crViewer.LogOnInfo.Count; i++)
            {
                crViewer.LogOnInfo[i].ConnectionInfo = conn;
            }
        }
    }
}